 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LINQ2Object.Data_Test;

namespace LINQ2Object
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // lấy giá trị hiện tại trên txtParam1 và txtParam2
            //int nParam1 = int.Parse(txtParam1.Text.ToString());
            //int nParam2 = int.Parse(txtParam2.Text.ToString());

            // xuất ra kết quả
            //List<int> nList = new List<int>
            //{
            //    nParam1,
            //    nParam2
            //};

            //var result = nList.Where(c => c > 20);
            //foreach (var r in result)
            //{
            //    txtResult.AppendText(r.ToString() + "\n");
            //}

            List<int> nListTest = new List<int>
            {
                13, 15, 56, 90, 100,
                15, 76, 89, 80, 102
            };

            // Đây là dòng thêm vào
            List<int> nAddList = new List<int>
            {
              0,0,100
            };
            var queryByExpression = from n in nListTest
                        where n % 2 == 0
                        select new
                        {
                            Value     = n,
                            Message   = "Query by expression"
                        };

            var queryByMethod = nListTest.Where(n => n % 2 == 0)
                                         .Select(n => new
                                         {
                                             Value   = n,
                                             Message = "Query by method"
                                         });
            txtResult.AppendText("Query theo biểu thức:\n");
            foreach (var r in queryByExpression)
            {
                txtResult.AppendText(r.Value + " " + r.Message + "\n");
            }

            txtResult.AppendText("\n\n");
            foreach (var r in queryByMethod)
            {
                txtResult.AppendText(r.Value + " " + r.Message + "\n");
            }

            //var query = from element in nListTest
            //            let count = nListTest.Count
            //            select (element - count);

            //var query = nListTest.Select(element => new { value = element, count = nListTest.Count })
            //                     .Select(element => element.value - element.count);
            //foreach (var element in query){
            //    txtResult.AppendText(element.ToString() + "\n");

                var query1 = from element1 in nListTest
                             group element1 by element1 into groups
                             select new
                             {
                                 key = groups.Key,
                                 count = groups.Count()
                             };
                foreach (var element1 in query1)
                {
                    txtResult.AppendText(element1.ToString() + "\n");
                }
                var sampleData = Contact.SampleData();
                var query3 = from element3 in sampleData
                             where element3.State == "WA"
                             orderby element3.LastName, element3.FirstName
                             select element3;
                foreach (var element3 in query3)
                {
                    txtResult.AppendText(element3.LastName + " " + element3.FirstName +  "\n");
                }
            nListTest.Join(1000);

            }

        }
    }
